package com.marchsoft.campushelp.service.impl;

import com.marchsoft.campushelp.domain.CampusTransferForm;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.marchsoft.campushelp.repository.CampusTransferFormRepository;
import com.marchsoft.campushelp.service.CampusTransferFormService;
import com.marchsoft.campushelp.service.dto.CampusTransferFormDto;
import com.marchsoft.campushelp.service.dto.CampusTransferFormQueryCriteria;
import com.marchsoft.campushelp.service.mapper.CampusTransferFormMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Service
//@CacheConfig(cacheNames = "campusTransferForm")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusTransferFormServiceImpl implements CampusTransferFormService {

    private final CampusTransferFormRepository campusTransferFormRepository;

    private final CampusTransferFormMapper campusTransferFormMapper;

    public CampusTransferFormServiceImpl(CampusTransferFormRepository campusTransferFormRepository, CampusTransferFormMapper campusTransferFormMapper) {
        this.campusTransferFormRepository = campusTransferFormRepository;
        this.campusTransferFormMapper = campusTransferFormMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusTransferFormQueryCriteria criteria, Pageable pageable){
        Page<CampusTransferForm> page = campusTransferFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusTransferFormMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusTransferFormDto> queryAll(CampusTransferFormQueryCriteria criteria){
        return campusTransferFormMapper.toDto(campusTransferFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusTransferFormDto findById(Long id) {
        CampusTransferForm campusTransferForm = campusTransferFormRepository.findById(id).orElseGet(CampusTransferForm::new);
        ValidationUtil.isNull(campusTransferForm.getId(),"CampusTransferForm","id",id);
        return campusTransferFormMapper.toDto(campusTransferForm);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusTransferFormDto create(CampusTransferForm resources) {
        return campusTransferFormMapper.toDto(campusTransferFormRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusTransferForm resources) {
        CampusTransferForm campusTransferForm = campusTransferFormRepository.findById(resources.getId()).orElseGet(CampusTransferForm::new);
        ValidationUtil.isNull( campusTransferForm.getId(),"CampusTransferForm","id",resources.getId());
        campusTransferForm.copy(resources);
        campusTransferFormRepository.save(campusTransferForm);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            campusTransferFormRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusTransferFormDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusTransferFormDto campusTransferForm : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", campusTransferForm.getUserId());
            map.put("转账类型", campusTransferForm.getTransferType());
            map.put("金额", campusTransferForm.getAmount());
            map.put("时间", campusTransferForm.getTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}